package com.iotproject.studyservice.controller;


import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyWord;
import com.iotproject.studyservice.entity.UserCollectionWord;
import com.iotproject.studyservice.entity.vo.CollectionWord;
import com.iotproject.studyservice.service.StudyWordService;
import com.iotproject.studyservice.service.UserCollectionWordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户收藏单词 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-04-06
 */
@RestController
@RequestMapping("/studyservice/user-collection-word")
public class UserCollectionWordController {
    //判断单词是否在用户收藏表里

    @Autowired
    UserCollectionWordService userCollectionWordService;

    @Autowired
    StudyWordService studyWordService;

    @GetMapping("isUserCollectionWord/{wordId}")
    public R isUserCollectionWord(@PathVariable("wordId") String wordId, HttpServletRequest httpServletRequest) {
        String memberUserId = studyWordService.getMemberUserId(httpServletRequest);
        QueryWrapper<UserCollectionWord> userCollectionWordQueryWrapper = new QueryWrapper<>();
        userCollectionWordQueryWrapper.eq("user_id", memberUserId);
        userCollectionWordQueryWrapper.eq("collection_word_id", wordId);
        int collectionWordRecord = userCollectionWordService.count(userCollectionWordQueryWrapper);
        if (collectionWordRecord == 0) {
            return R.ok().data("isUserCollectionWord", false);
        }
        return R.ok().data("isUserCollectionWord", true);
    }

    //加入用户单词收藏夹
    @GetMapping("addToFavorites/{wordId}")
    public R addToFavorites(@PathVariable("wordId") String wordId, HttpServletRequest httpServletRequest) {
        String memberUserId = studyWordService.getMemberUserId(httpServletRequest);
        StudyWord studyWord = studyWordService.getById(wordId);
        String grade = studyWord.getGrade();
        UserCollectionWord userCollectionWord = new UserCollectionWord();
        userCollectionWord.setUserId(memberUserId);
        userCollectionWord.setCollectionWordId(wordId);
        userCollectionWord.setGrade(grade);
        boolean save = userCollectionWordService.save(userCollectionWord);
        return R.ok();
    }

    //获取收藏夹单词列表
    @GetMapping("getUserCollectionWordList/{grade}")
    public R getUserCollectionWordList(@PathVariable("grade") String grade, HttpServletRequest httpServletRequest) {
        String memberUserId = studyWordService.getMemberUserId(httpServletRequest);
        List<CollectionWord> userCollectionWordList = userCollectionWordService.getUserCollectionWordList(memberUserId, grade);
        return R.ok().data("list", userCollectionWordList);
    }

    //删除用户收藏夹单词
    @DeleteMapping("delectUserCollectionWord/{wordId}")
    public R delectUserCollectionWord(@PathVariable("wordId") String wordId) {
        boolean delectUserCollectionWord = userCollectionWordService.removeById(wordId);
        if (delectUserCollectionWord) {
            return R.ok();
        }
        return R.error();
    }


}

